<?php
namespace App\Http\Controllers\Banner;

use App\Http\Controllers\Controller;
use App\Libraries\Decorator\Image\CoverDecorator;
use App\Models\Banner\Banner;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;


class BannerController extends Controller{


    protected $banner;
    protected $coverDecorator;

    public function __construct(Banner $banner, CoverDecorator $coverDecorator){
        $this->banner = $banner;
        $this->coverDecorator = $coverDecorator;
    }

    /**
     * banner
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function banners(Request $request){
        $validator = Validator::make($request->all(), [
            'position' => 'required|integer',
            'city_id' => 'integer',
        ], [
            'position.required' => '10001',
            'position.integer' => '10002',
            'city_id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $this->banner->addDecorator($this->coverDecorator);
        $city_id = $request['city_id']??0;
        $data = $this->banner->getBannerByPosition($request['position'], $city_id);
        return $this->sendSuccessResponse($data);
    }

}